package com.yyj.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.yyj.dao.TPropertyPaymentMapper;
import com.yyj.dao.TPropertyWaterBillMapper;
import com.yyj.enums.PaymentStatusEnum;
import com.yyj.enums.PaymentTypeEnum;
import com.yyj.exception.BaseException;
import com.yyj.model.*;
import com.yyj.pojo.user.SessionInfo;
import com.yyj.util.DateUtil;
import com.yyj.web.controller.BaseController;
import com.yyj.web.protocol.PageRequest;
import com.yyj.web.protocol.PageRspData;
import com.yyj.web.protocol.req.*;
import com.yyj.web.protocol.resp.BackendWaterBillListResp;
import com.yyj.web.protocol.resp.PaymentQueryResp;
import com.yyj.web.protocol.resp.WaterBillInfoResp;
import com.yyj.web.protocol.resp.WaterBillQueryResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class WaterBillService {

    public static final Logger logger = LoggerFactory.getLogger(WaterBillService.class);

    @Autowired
    private TPropertyWaterBillMapper waterBillMapper;

    @Autowired
    private TPropertyPaymentMapper paymentMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private FeeConfigService feeConfigService;

    @Autowired
    private PushMessageService pushMessageService;

    public boolean checkWaterBill(SessionInfo sessionInfo) throws Exception {
        String currentMonth = DateUtil.getNowShortDate().substring(0, 6);
        WaterBillQueryReq waterBillQueryReq = new WaterBillQueryReq();
        waterBillQueryReq.setRoomNum(sessionInfo.getRoomNum());
        waterBillQueryReq.setFeeDate(currentMonth);
        List<TPropertyWaterBill> waterBillList = waterBillMapper.queryWaterBill(waterBillQueryReq);
        if (waterBillList.size() > 0) {
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    public WaterBillInfoResp fillWaterBill(WaterBillAddReq waterBillAddReq, SessionInfo sessionInfo) throws Exception{
        WaterBillInfoResp waterBillInfoResp = new WaterBillInfoResp();
        String currentMonth = DateUtil.getNowShortDate().substring(0 , 6);
        WaterBillQueryReq waterBillQueryReq = new WaterBillQueryReq();
        waterBillQueryReq.setRoomNum(sessionInfo.getRoomNum());
        waterBillQueryReq.setFeeDate(currentMonth);
        //waterBillQueryReq.setStatus(PaymentStatusEnum.AUDITING.getCode());
        List<TPropertyWaterBill> waterBillList = waterBillMapper.queryWaterBill(waterBillQueryReq);
        if (waterBillList.size() > 0) {
            throw new BaseException("当月该房间已提交过水费记录！");
        }

        TPropertyWaterBill waterBill = new TPropertyWaterBill();

        waterBill.setRoomNum(sessionInfo.getRoomNum());
        waterBill.setCreator(sessionInfo.getUserId());
        waterBill.setImgUrl1(waterBillAddReq.getImgUrl1());
        waterBill.setImgUrl2(waterBillAddReq.getImgUrl2());
        waterBill.setCreateTime(new Date());
        waterBill.setUpdateTime(new Date());
        waterBill.setStatus(PaymentStatusEnum.AUDITING.getCode());
        waterBill.setMobile(sessionInfo.getMobile());
        waterBill.setFeeDate(currentMonth);
        double sumReadNum = 0D;
        String multiReadNum = waterBillAddReq.getReadNum();
        waterBill.setMultiReadNum(multiReadNum);
        if(multiReadNum.contains("/")){
            String [] multiReadNumArr = multiReadNum.split("/");
            for(String readNumStr : multiReadNumArr){
                sumReadNum = sumReadNum + Double.valueOf(readNumStr);
            }
        }else{
            sumReadNum = Double.valueOf(multiReadNum);
        }
        waterBill.setReadNum(new BigDecimal(sumReadNum));
        int insertRlt = waterBillMapper.insert(waterBill);
        if(1 == insertRlt){
            TPropertyWaterBillExample waterBillExample = new TPropertyWaterBillExample();
            TPropertyWaterBillExample.Criteria criteria = waterBillExample.createCriteria();
            String previewDate = DateUtil.getSpecifiedMonthBefore(DateUtil.getCurrDate(),-1);
            //criteria.andFeeDateEqualTo(previewDate.substring(0 , 4) + previewDate.substring(5 ,7));
            criteria.andRoomNumEqualTo(sessionInfo.getRoomNum());
            criteria.andStatusEqualTo(PaymentStatusEnum.IS_PAY.getCode());
            waterBillExample.setOrderByClause("create_time DESC");
            List<TPropertyWaterBill> list = waterBillMapper.selectByExample(waterBillExample);
            BigDecimal previewReadNum = BigDecimal.ZERO;
            Date previewSubmitDate = new Date();
            if(list.size() > 0) {
                TPropertyWaterBill tPropertyWaterBill = list.get(0);
                previewReadNum = tPropertyWaterBill.getReadNum();
                previewSubmitDate = tPropertyWaterBill.getCreateTime();
            }
            BigDecimal num = new BigDecimal(sumReadNum - previewReadNum.doubleValue());
            if(num.compareTo(BigDecimal.ZERO) < 0){
                throw new BaseException("当前水表读数不能小于上个月的读数");
            }
            TPropertyFeeConfig feeConfig = feeConfigService.findFeeConfigByType(PaymentTypeEnum.WATER_FEE.getCode());
            BigDecimal waterFee = feeConfig.getFee().multiply(num).setScale(2,   BigDecimal.ROUND_HALF_UP);
            PaymentAddReq paymentAddReq = new PaymentAddReq();
            paymentAddReq.setType(PaymentTypeEnum.WATER_FEE.getCode());
            paymentAddReq.setStatus(PaymentStatusEnum.AUDITING.getCode());
            paymentAddReq.setFeeDate(currentMonth);
            paymentAddReq.setAmount(waterFee);
            paymentAddReq.setRoomNum(sessionInfo.getRoomNum());
            paymentAddReq.setMobile(sessionInfo.getMobile());
            paymentAddReq.setWaterBillId(waterBill.getId());
            paymentService.addPayment(paymentAddReq);

            waterBillInfoResp.setStatus(PaymentStatusEnum.AUDITING.getCode());
            waterBillInfoResp.setFeeDate(currentMonth);
            waterBillInfoResp.setFee(waterFee.doubleValue());
            waterBillInfoResp.setCurrentReadNum(waterBill.getReadNum().doubleValue());
            waterBillInfoResp.setCurrentSubmitTime(new Date());
            waterBillInfoResp.setPreviewReadNum(previewReadNum.doubleValue());
            waterBillInfoResp.setPreviewSubmitTime(previewSubmitDate);

        }

        return waterBillInfoResp;

    }

    @Transactional(rollbackFor = Exception.class)
    public WaterBillInfoResp updateWaterBill(WaterBillUpdateReq waterBillAddReq, SessionInfo sessionInfo) throws Exception {
        WaterBillInfoResp waterBillInfoResp = new WaterBillInfoResp();
        /**上个月读数*/
        BigDecimal previewReadNum = BigDecimal.ZERO;
        /**上个月提交时间*/
        Date previewSubmitDate = new Date();
        /**当月水费*/
        BigDecimal waterFee = null;
        TPropertyWaterBill waterBillObj = waterBillMapper.selectByPrimaryKey(waterBillAddReq.getCurrentId());
        if (null != waterBillObj) {
            if(PaymentStatusEnum.NO_PAY.getCode().equals(waterBillObj.getStatus())
                    || PaymentStatusEnum.IS_PAY.getCode().equals(waterBillObj.getStatus())
                    ){
                throw new BaseException("该记录不是审核状态，无法修改");
            }
            double sumReadNum = 0D;
            String multiReadNum = waterBillAddReq.getReadNum();
            waterBillObj.setMultiReadNum(multiReadNum);
            if(multiReadNum.contains("/")){
                String [] multiReadNumArr = multiReadNum.split("/");
                for(String readNumStr : multiReadNumArr){
                    sumReadNum = sumReadNum + Double.valueOf(readNumStr);
                }
            }else{
                sumReadNum = Double.valueOf(multiReadNum);
            }
            waterBillObj.setReadNum(new BigDecimal(sumReadNum));
            waterBillObj.setUpdateTime(new Date());
            waterBillObj.setImgUrl1(waterBillAddReq.getImgUrl1());
            waterBillObj.setImgUrl2(waterBillAddReq.getImgUrl2());
            waterBillObj.setStatus(PaymentStatusEnum.AUDITING.getCode());
            waterBillMapper.updateByPrimaryKey(waterBillObj);

            TPropertyPaymentExample paymentExample = new TPropertyPaymentExample();
            TPropertyPaymentExample.Criteria criteria1 = paymentExample.createCriteria();
            criteria1.andTypeEqualTo(PaymentTypeEnum.WATER_FEE.getCode());
            criteria1.andRoomNumEqualTo(waterBillObj.getRoomNum());
            criteria1.andFeeDateEqualTo(DateUtil.getNowShortDate().substring(0, 6));
            criteria1.andStatusNotEqualTo(PaymentStatusEnum.NO_PAY.getCode());
            criteria1.andStatusNotEqualTo(PaymentStatusEnum.IS_PAY.getCode());
            List<TPropertyPayment> paymentList = paymentMapper.selectByExample(paymentExample);
            if(null != paymentList && paymentList.size() > 0) {
                TPropertyPayment rejectPayment = paymentList.get(0);
                rejectPayment.setStatus(PaymentStatusEnum.AUDITING.getCode());
                rejectPayment.setUpdateTime(new Date());

                //查询上个月记录
                TPropertyWaterBillExample waterBillExample = new TPropertyWaterBillExample();
                TPropertyWaterBillExample.Criteria criteria = waterBillExample.createCriteria();
                String previewDate = DateUtil.getSpecifiedMonthBefore(DateUtil.getCurrDate(),-1);
                criteria.andFeeDateEqualTo(previewDate.substring(0 , 4) + previewDate.substring(5 ,7));
                criteria.andRoomNumEqualTo(sessionInfo.getRoomNum());
                List<TPropertyWaterBill> list = waterBillMapper.selectByExample(waterBillExample);

                if(list.size() > 0) {
                    TPropertyWaterBill tPropertyWaterBill = list.get(0);
                    previewReadNum = tPropertyWaterBill.getReadNum();
                    previewSubmitDate = tPropertyWaterBill.getCreateTime();
                }
                BigDecimal num = new BigDecimal(sumReadNum - previewReadNum.doubleValue());
                if(num.compareTo(BigDecimal.ZERO) < 0){
                    throw new BaseException("当前水表读数不能小于上个月的读数");
                }
                TPropertyFeeConfig feeConfig = feeConfigService.findFeeConfigByType(PaymentTypeEnum.WATER_FEE.getCode());
                waterFee = feeConfig.getFee().multiply(num).setScale(2,   BigDecimal.ROUND_HALF_UP);
                rejectPayment.setAmount(waterFee);
                paymentMapper.updateByPrimaryKeySelective(rejectPayment);
            }
            waterBillInfoResp.setStatus(PaymentStatusEnum.AUDITING.getCode());
            waterBillInfoResp.setFeeDate(DateUtil.getNowShortDate().substring(0 , 6));
            waterBillInfoResp.setFee(waterFee.doubleValue());
            waterBillInfoResp.setCurrentReadNum(sumReadNum);
            waterBillInfoResp.setCurrentSubmitTime(new Date());
            waterBillInfoResp.setPreviewReadNum(previewReadNum.doubleValue());
            waterBillInfoResp.setPreviewSubmitTime(previewSubmitDate);
        }

        return waterBillInfoResp;
    }

    @Transactional(rollbackFor = Exception.class)
    public TPropertyWaterBill auditWaterBill(WaterBillAuditReq waterBillAuditReq , SessionInfo sessionInfo) throws Exception {
        TPropertyWaterBill waterBillObj = waterBillMapper.selectByPrimaryKey(waterBillAuditReq.getWaterBillId());
        if(null == waterBillObj){
            throw new BaseException("水表记录不存在");
        }
        waterBillObj.setStatus(PaymentStatusEnum.REJECTED.getCode().equals(waterBillAuditReq.getStatus()) ?
                PaymentStatusEnum.REJECTED.getCode() : PaymentStatusEnum.NO_PAY.getCode()
            );
        waterBillObj.setUpdateTime(new Date());
        waterBillObj.setRemark(waterBillAuditReq.getRemark());
        waterBillMapper.updateByPrimaryKeySelective(waterBillObj);

        TPropertyPaymentExample paymentExample = new TPropertyPaymentExample();
        TPropertyPaymentExample.Criteria criteria = paymentExample.createCriteria();
        criteria.andTypeEqualTo(PaymentTypeEnum.WATER_FEE.getCode());
        criteria.andRoomNumEqualTo(waterBillObj.getRoomNum());
        criteria.andFeeDateEqualTo(DateUtil.getNowShortDate().substring(0, 6));
        criteria.andStatusEqualTo(PaymentStatusEnum.AUDITING.getCode());
        List<TPropertyPayment> paymentList = paymentMapper.selectByExample(paymentExample);
        if(null != paymentList && paymentList.size() > 0) {
            TPropertyPayment payment = paymentList.get(0);
            if(PaymentStatusEnum.REJECTED.getCode().equals(waterBillAuditReq.getStatus())){
                payment.setAmount(null);
                payment.setStatus(PaymentStatusEnum.REJECTED.getCode());
            }else{
                payment.setStatus(PaymentStatusEnum.NO_PAY.getCode());
                payment.setAuditTime(new Date());
            }
            payment.setUpdateTime(new Date());
            paymentMapper.updateByPrimaryKeySelective(payment);
        }

        String auditResult = "";
        if(PaymentStatusEnum.REJECTED.getCode().equals(waterBillAuditReq.getStatus())) {
            auditResult = "申请被驳回";
        }
        if(PaymentStatusEnum.PASSED.getCode().equals(waterBillAuditReq.getStatus())) {
            auditResult = "申请通过";
        }
        PushMsgWaterBillReq pushMsgWaterBillReq = new PushMsgWaterBillReq();
        pushMsgWaterBillReq.setWaterBillId(waterBillObj.getId());
        pushMsgWaterBillReq.setStatus(auditResult);
        pushMsgWaterBillReq.setCreateTime(new Date());
        pushMsgWaterBillReq.setFeeDate(waterBillObj.getFeeDate());
        pushMsgWaterBillReq.setMobile(waterBillObj.getMobile());
        pushMsgWaterBillReq.setContact(sessionInfo.getMobile());
        pushMsgWaterBillReq.setRemark(waterBillObj.getRemark());
        pushMessageService.pushWaterResultToOwners(pushMsgWaterBillReq);

        return waterBillObj;
    }

    public WaterBillInfoResp queryWaterBillInfo(WaterBillQueryReq waterBillQueryReq) throws Exception {
        WaterBillInfoResp waterBillInfoResp = new WaterBillInfoResp();
        List<TPropertyWaterBill> waterBillList = waterBillMapper.queryWaterBill(waterBillQueryReq);
        if (waterBillList.size() == 0) {
            return waterBillInfoResp;
        }
        if (waterBillList.size() > 0) {
            TPropertyWaterBill currentWaterBill = waterBillList.get(0);
            waterBillInfoResp.setCurrentId(currentWaterBill.getId());
            waterBillInfoResp.setStatus(currentWaterBill.getStatus());
            waterBillInfoResp.setRemark(currentWaterBill.getRemark());
            waterBillInfoResp.setCurrentReadNum(currentWaterBill.getReadNum().doubleValue());
            waterBillInfoResp.setCurrentSubmitTime(currentWaterBill.getCreateTime());
            waterBillInfoResp.setUpdateFlag(0);
            if( null != currentWaterBill.getUpdateFlag() && 1 == currentWaterBill.getUpdateFlag()){
                waterBillInfoResp.setUpdateFlag(1);
                Date updateTime = currentWaterBill.getUpdateTime();
                waterBillInfoResp.setManagerUpdateMsg("管理员在" + DateUtil.getDateMD(updateTime) + "修改抄表数据为" + currentWaterBill.getReadNum().doubleValue());
            }
            /**查询缴费金额*/
            PaymentQueryReq paymentQueryReq = new PaymentQueryReq();
            paymentQueryReq.setType(PaymentTypeEnum.WATER_FEE.getCode());
            paymentQueryReq.setRoomNum(waterBillQueryReq.getRoomNum());
            paymentQueryReq.setFeeDate(DateUtil.getNowShortDate().substring(0, 6));
            PaymentQueryResp paymentObj = paymentService.findPaymentRecord(paymentQueryReq);
            if(null != paymentObj) {
                waterBillInfoResp.setFee(paymentObj.getAmount().doubleValue());
                String feeDate = paymentObj.getFeeDate();
                String feeMonth = feeDate.substring(4 , 6).startsWith("0") ? feeDate.substring(5 , 6): feeDate.substring(4 , 6);
                String feeDate_ = feeDate.substring(0 , 4) + "年" + feeMonth + "月";
                waterBillInfoResp.setFeeDate(feeDate_);
            }
        }
        TPropertyWaterBillExample waterBillExample = new TPropertyWaterBillExample();
        TPropertyWaterBillExample.Criteria criteria = waterBillExample.createCriteria();
        criteria.andRoomNumEqualTo(waterBillQueryReq.getRoomNum());
        criteria.andStatusEqualTo(PaymentStatusEnum.IS_PAY.getCode());
        waterBillExample.setOrderByClause("create_time DESC");
        List<TPropertyWaterBill> list = waterBillMapper.selectByExample(waterBillExample);
        if(list.size() > 0) {
            TPropertyWaterBill tPropertyWaterBill = list.get(0);
            waterBillInfoResp.setPreviewSubmitTime(tPropertyWaterBill.getCreateTime());
            waterBillInfoResp.setPreviewReadNum(tPropertyWaterBill.getReadNum().doubleValue());
            waterBillInfoResp.setPreviewId(tPropertyWaterBill.getId());
        }

        return waterBillInfoResp;
    }

    public PageRspData<WaterBillQueryResp> queryUnpaidWaterBill(WaterBillQueryReq waterBillQueryReq) throws Exception {
        Page page = PageHelper.startPage(waterBillQueryReq.getCurrentPage(), waterBillQueryReq.getPageSize());
        List<WaterBillQueryResp> list = waterBillMapper.queryUnpaidWaterBill(waterBillQueryReq);
        List<WaterBillQueryResp> newList = Lists.newArrayList();
        for(WaterBillQueryResp waterBillQueryResp : list){
            if(null != waterBillQueryResp.getUpdateFlag() && 1 == waterBillQueryResp.getUpdateFlag()){
                waterBillQueryResp.setManagerUpdateMsg("管理员在" + DateUtil.getDateMD(waterBillQueryResp.getUpdateTime()) + "修改抄表数据为" + waterBillQueryResp.getReadNum());
            }
            newList.add(waterBillQueryResp);
        }
        PageRspData<WaterBillQueryResp> pageRspData = new PageRspData<WaterBillQueryResp>(
                page.getTotal(), waterBillQueryReq.getCurrentPage(), waterBillQueryReq.getPageSize(), newList);
        return pageRspData;
    }

    public TPropertyWaterBill queryWaterBillDetail(long id){
        return waterBillMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public WaterBillInfoResp managerUpdateWaterBill(WaterBillManagerUpdateReq waterBillUpdateReq) throws Exception {
        WaterBillInfoResp waterBillInfoResp = new WaterBillInfoResp();
        TPropertyWaterBill tPropertyWaterBill = waterBillMapper.selectByPrimaryKey(waterBillUpdateReq.getCurrentId());
        double sumReadNum = 0D;
        String multiReadNum = waterBillUpdateReq.getReadNum();
        tPropertyWaterBill.setMultiReadNum(multiReadNum);
        if(multiReadNum.contains("/")){
            String [] multiReadNumArr = multiReadNum.split("/");
            for(String readNumStr : multiReadNumArr){
                sumReadNum = sumReadNum + Double.valueOf(readNumStr);
            }
        }else{
            sumReadNum = Double.valueOf(multiReadNum);
        }
        tPropertyWaterBill.setReadNum(new BigDecimal(sumReadNum).setScale(2 , BigDecimal.ROUND_HALF_UP));
        tPropertyWaterBill.setUpdateFlag(1);
        tPropertyWaterBill.setUpdateTime(new Date());
        int updateRlt = waterBillMapper.updateByPrimaryKeySelective(tPropertyWaterBill);
        if(1 == updateRlt){
            waterBillInfoResp.setUpdateFlag(1);
            Date updateTime = tPropertyWaterBill.getUpdateTime();
            waterBillInfoResp.setManagerUpdateMsg("管理员在" + DateUtil.getDateMD(updateTime) + "修改抄表数据为" + tPropertyWaterBill.getReadNum());
            waterBillInfoResp.setCurrentReadNum(tPropertyWaterBill.getReadNum().doubleValue());

            TPropertyPayment tPropertyPayment = paymentService.findPaymentByWaterBillId(waterBillUpdateReq.getCurrentId());
            if(null != tPropertyPayment){
                TPropertyFeeConfig feeConfig = feeConfigService.findFeeConfigByType(PaymentTypeEnum.WATER_FEE.getCode());
                BigDecimal waterFee = feeConfig.getFee().multiply(new BigDecimal(sumReadNum)).setScale(2,   BigDecimal.ROUND_HALF_UP);
                tPropertyPayment.setAmount(waterFee);
                tPropertyPayment.setUpdateTime(updateTime);
                paymentMapper.updateByPrimaryKeySelective(tPropertyPayment);
            }
        }
        return waterBillInfoResp;
    }

    public PageRspData<BackendWaterBillListResp> queryBackendWaterBillList(BackendWaterBillListReq req) throws Exception {
        Page page = PageHelper.startPage(req.getCurrentPage(), req.getPageSize());
        List<BackendWaterBillListResp> list = waterBillMapper.queryBackendWaterBill(req);
        PageRspData<BackendWaterBillListResp> pageRspData = new PageRspData<BackendWaterBillListResp>(
                page.getTotal(), req.getCurrentPage(), req.getPageSize(), list);
        return pageRspData;
    }

    public WaterBillInfoResp queryWaterBillInfo(Long waterBillId) throws Exception {
        WaterBillInfoResp waterBillInfoResp = new WaterBillInfoResp();
        TPropertyWaterBill currentWaterBill = waterBillMapper.selectByPrimaryKey(waterBillId);
        waterBillInfoResp.setCurrentId(currentWaterBill.getId());
        waterBillInfoResp.setStatus(currentWaterBill.getStatus());
        waterBillInfoResp.setRemark(currentWaterBill.getRemark());
        waterBillInfoResp.setCurrentReadNum(currentWaterBill.getReadNum().doubleValue());
        waterBillInfoResp.setCurrentSubmitTime(currentWaterBill.getCreateTime());
        waterBillInfoResp.setUpdateFlag(0);
        if( null != currentWaterBill.getUpdateFlag() && 1 == currentWaterBill.getUpdateFlag()){
            waterBillInfoResp.setUpdateFlag(1);
            Date updateTime = currentWaterBill.getUpdateTime();
            waterBillInfoResp.setManagerUpdateMsg("管理员在" + DateUtil.getDateMD(updateTime) + "修改抄表数据为" + currentWaterBill.getReadNum().doubleValue());
        }
        /**查询缴费金额*/
        PaymentQueryReq paymentQueryReq = new PaymentQueryReq();
        paymentQueryReq.setType(PaymentTypeEnum.WATER_FEE.getCode());
        paymentQueryReq.setRoomNum(currentWaterBill.getRoomNum());
        paymentQueryReq.setFeeDate(currentWaterBill.getFeeDate());
        PaymentQueryResp paymentObj = paymentService.findPaymentRecord(paymentQueryReq);
        if(null != paymentObj) {
            waterBillInfoResp.setFee(paymentObj.getAmount().doubleValue());
            String feeDate = paymentObj.getFeeDate();
            String feeMonth = feeDate.substring(4 , 6).startsWith("0") ? feeDate.substring(5 , 6): feeDate.substring(4 , 6);
            String feeDate_ = feeDate.substring(0 , 4) + "年" + feeMonth + "月";
            waterBillInfoResp.setFeeDate(feeDate_);
        }
        TPropertyWaterBillExample waterBillExample = new TPropertyWaterBillExample();
        TPropertyWaterBillExample.Criteria criteria = waterBillExample.createCriteria();
        //String previewDate = DateUtil.getSpecifiedMonthBefore(DateUtil.getCurrDate(),-1);
        criteria.andRoomNumEqualTo(currentWaterBill.getRoomNum());
        criteria.andStatusEqualTo(PaymentStatusEnum.IS_PAY.getCode());
        waterBillExample.setOrderByClause("create_time DESC");
        List<TPropertyWaterBill> list = waterBillMapper.selectByExample(waterBillExample);
        if(list.size() > 0) {
            TPropertyWaterBill tPropertyWaterBill = list.get(0);
            waterBillInfoResp.setPreviewSubmitTime(tPropertyWaterBill.getCreateTime());
            waterBillInfoResp.setPreviewReadNum(tPropertyWaterBill.getReadNum().doubleValue());
            waterBillInfoResp.setPreviewId(tPropertyWaterBill.getId());
        }
        return waterBillInfoResp;
    }


    public static void main(String[] args) {
        String previewDate = DateUtil.getSpecifiedMonthBefore(DateUtil.getCurrDate(),-1);
        //System.out.println(previewDate);
        //System.out.println(previewDate.substring(0 , 4) + previewDate.substring(5 ,7));
        String feeDate = "201805";
        String feeMonth = feeDate.substring(4 , 6).startsWith("0") ? feeDate.substring(5 , 6): feeDate.substring(4 , 6);
        String feeDate_ = feeDate.substring(0 , 4) + "年" + feeMonth + "月";
        System.out.println(feeDate_);

        System.out.println(DateUtil.getDateMD(new Date()));
    }
}